package com.zhongshengnetwork.rightbe.Adv.model;

import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 启动页广告
 */

public class AdvertUtils {

    public static final String AdvBgUrl = "AdvBgUrl";
    public static final String AdvContentUrl = "AdvContentUrl";
    public static final String AdvStatus = "AdvStatus";
    public static final String AdvImgPath = "AdvImgPath";

    public static final String StartGif = "StartGif";

    public static final String HomeBgUrl = "HomeBgUrl";
    public static final String HomeContentUrl = "HomeContentUrl";
    public static final String HomeStatus = "HomeStatus";
    public static final String HomeImgPath = "HomeImgPath";

    public static final String HomeData = "HomeData";
    public static final String CareData = "CareData";
    public static final String HotData = "HotData";
    public static final String LatestData = "LatestData";
    public static final String NewData = "NewData";
    public static final String NewWeijuData = "NewWeijuData";
    public static final String NewLifeData = "NewLifeData";
    public static final String NewTopicData = "NewTopicData";
    public static final String NewHeartData = "NewHeartData";
    public static final String NewBijiData = "NewBijiData";

    public static final String DiscoverData = "DiscoverData";
    public static final String OriginalData = "OriginalData";
    public static final String PopularData = "PopularData";
    public static final String CircleData = "CircleData";
    public static final String HotCommentData = "HotCommentData";
    public static final String LatestCommentData = "LatestCommentData";
    public static final String BannerData = "BannerData";//微句轮播
    public static final String HomeBannerData = "HomeBannerData"; //首页轮播数据
    public static final String HomeMenuData = "HomeMenuData"; //首页菜单
    public static final String CommentBannerData = "CommentBannerData";
    public static final String UserSignTime = "UserSignTime";
    public static final String LatestTopicData = "LatestTopicData";
    public static final String HotTopicData = "HotTopicData";
    public static final String LifeShareData = "LifeShareData";
    public static final String HeartWordData = "HeartWordData";
    public static final String BestComment = "BestComment";
    public static final String UserReadData = "UserReadData";
    public static final String FindBannerData = "FindBannerData";//互动轮播
    public static final String WZTUserLabelData = "WZTUserLabelData";//微纸条标签
    public static final String FirstViewLoadFlag = "FirstViewLoadFlag"; //首次加载
    public static final String UserLevelNameData = "UserLevelNameData"; //登录用户的等级名称
    public static final String WZTBannerData = "WZTBannerData"; //微纸条轮播
    public static final String XFJZBannerData = "XFJZBannerData";//想法句子

    public static final String CareLangID = "CareLangID"; //记录id
    public static final String BookmarkID = "BookmarkID"; //广场收藏夹
    public static final String WZTFindID = "WZTFindID"; //微纸条发现列表

    //微句频道
    public static final String MyChannelDataKey = "MyChannelData";//我选中的频道（仅保存逗号分隔的id）
    public static final String MyChannelNameDataKey = "MyChannelNameData";//我选中的频道名称（逗号分隔）
    public static final String NewChannelDataKey = "NewChannelData"; //系统推荐的频道（仅保存逗号分隔的id）
    public static final String NewChannelNameDataKey = "NewChannelNameDataKey"; //系统推荐的频道名称

    public static final String DefaultFindSolidChannelNameData = "热门,原创";
    public static final String DefaultFindSolidChannelData = "-6,-7";

    public static final String DefaultSolidChannelNameData = "关注,推荐,微句,微纸条"; //,文案
    public static final String DefaultSolidChannelData = "-3,-1,-2,-5"; //,-4
    public static final int DefaultSolidCount = 5;
    public static final String DefaultChannelNameData = "生活分享,话题探索,心里话,笔记,优秀笔记";//,影视金句,心情签名,精辟短句,经典语录,名人金句,感悟句子,正能量,情感句子,幽默诙谐,诗词改编,伤感句子,优美句子,情商瞬间,创意句子,哲理句子,英文句子,古风句子,意境句子,温馨句子
    public static final String DefaultChannelData = "30,21,44,116,117";//,52,45,23,15,1,8,11,29,14,4,46,16,18,27,28,31,33,41,47

    //public static final String DefaultMySolidChannelDataName = "经典语录,名人金句,感悟句子,正能量,创意句子";
    //public static final String DefaultMySolidChannelData = "15,1,8,11,27";

    //微纸条频道
    public static final String MyWZTChannelDataKey = "MyWZTChannelDataKey";
    public static final String MyWZTChannelNameDataKey = "MyWZTChannelNameDataKey";
    public static final String NewWZTChannelDataKey = "NewWZTChannelDataKey";
    public static final String NewWZTChannelNameDataKey = "NewWZTChannelNameDataKey"; //系统推荐的频道名称
    public static final String DefaultWZTChannelNameData = "感悟,经验,影评,书评,台词,素材,读书笔记,情感,英文,幽默,心情签名,名人金句,小故事,文案"; //不选择的时候，如果平台的内容也为空的时候，默认显示的内容
    public static final String DefaultWZTChannelData = "3,2,6,7,8,9,17,27,29,30,31,35,36,39"; //不选择的时候，如果平台的内容也为空的时候，默认显示的内容

    //1，登录成功后获取；2，获取到类型后获取（封装成一个异步方法）
    //排序：(1)DefaultSolidChannelNameData(固定，客户端固定)->NewChannelNameDataKey（固定，系统推荐）->DefaultChannelNameData（可变，用户自定义）
    //     (2)DefaultSolidChannelNameData（固定，客户端固定）->NewChannelNameDataKey（固定，系统推荐）->MyChannelNameDataKey (可变，用户自定义)
    public static final String DeviceID = "DeviceID";

    public static final String LangTypeName = "LangTypeNameAll";

    public static final String CommonTypeName = "CommonTypeName";//常用发布类型

    public static final String ThirdDataName = "ThirdDataName";

    public static final String JPUSHRegistrationId = "JPUSHRegistrationId";

    public static final String LastGetUnReadMsgTime = "LastGetUnReadMsgTime";

    public static final String NoNameKey = "NoNameKey";

    public static final String CareUpdateKey = "CareUpdateKey";

    //阅读时长记录
    public static final String ReadTimeKey = "ReadTimeKey";
    public static final String LastRecordTimeKey = "LastRecordTimeKey";

    //新消息通知
    public static final String NewMsgTipKey = "NewMsgTipKey";

    //新的信息
    public static final String NewMsgNotifyKey = "NewMsgNotifyKey";

    //评价信息
    public static final String CommentAppKey = "CommentAppKey";

    //文案信息
    public static final String ClertData = "ClertData";
    public static final String HomeClertData = "HomeClertData";

    //动态评论墙
    public static final String MoveCommentKey = "MoveCommentKey";

    //白名单
    public static final String whiteKey = "whiteKey";

    //原创金句
    public static final String OriginalBestLangIDKey = "OriginalBestLangIDKey";
    public static final String OriginalBestLangTipKey = "OriginalBestLangTipKey";
    //每日推荐
    public static final String DailyRecommendLangIDKey = "DailyRecommendLangIDKey";
    public static final String DailyRecommendLangTipKey = "DailyRecommendLangTipKey";
    //精选文案
    public static final String BestClerkIDKey = "BestClerkIDKey";

    //UI模式
    public static final String UIModeKey = "UIModeKey";  //0-标准模式；1-个性模式
    public static final int UI_Normal_Mode = 0;
    public static final int UI_Simple_Mode = 1;

    //个性模式下，首页的菜单选择
    public static final String HomeLatestMenuKey = "HomeLatestMenuKey";
    public static final int All_Content_Mode = 0;//全部
    public static final int Weiju_Content_Mode = 1;//微句
    public static final int Life_Content_Mode = 2; //生活
    public static final int Topic_Content_Mode = 3; //话题
    public static final int Heart_Content_Mode = 4;//心里话
    public static final int My_Content_Mode = 5; //我的频道
    public static final int Biji_Content_Mode = 6; //笔记

    //智慧排行开启状态
    public static final String Smart_Rank_State = "SmartRankState";

    //签到
    public static final String SignData = "SignData";


    //评论排序
    public static final String CommentOrderKey = "CommentOrderKey"; //0-最新；1-最热

    //封面滤镜
    public static final String FilterCoverKey = "FilterCoverKey";

    //微句发布草稿
    public static final String WeiJuDraftKey = "WeiJuDraftKey";

    //个人中心信息获取时间
    public static final String GetMyInfoKey = "GetMyInfoKey";

    //复制标识
    public static final String CopyCountKey = "CopyCountKey";

    //上一次复制的内容
    public static final String LastCopyKey = "LastCopyKey";

    /***
     * down file
     *
     * @return
     * @throws MalformedURLException
     */
    public static void onSaveBitmap(String down_url, String file, Context context, String key)throws Exception {
                int down_step = 3;// 提示step
                int totalSize;// 文件总大小
                int downloadCount = 0;// 已经下载好的大小
                int updateCount = 0;// 已经上传的文件大小
                int TIMEOUT = 10 * 1000;// 超时
                InputStream inputStream;
                OutputStream outputStream;
                URL url = new URL(down_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(TIMEOUT);
                httpURLConnection.setReadTimeout(TIMEOUT);
                // 获取下载文件的size
                totalSize = httpURLConnection.getContentLength();

                if (httpURLConnection.getResponseCode() == 404) {
                    Log.e("","下载广告图失败");
                    throw new Exception("fail!");
                    //这个地方应该加一个下载失败的处理，但是，因为我们在外面加了一个try---catch，已经处理了Exception,
                    //所以不用处理
                }

                inputStream = httpURLConnection.getInputStream();
                outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉

                byte buffer[] = new byte[1024];
                int readsize = 0;
        while ((readsize = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, readsize);
            downloadCount += readsize;// 时时获取下载到的大小
            /*** 每次增张3%**/
            if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) {
                updateCount += down_step;
            }
        }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                inputStream.close();
                outputStream.close();
                SPManageUtils.getInstance(context).putSPString(key,file);
        Log.e("","下载广告图结束");

    }


    /**
     * @param localFilePath
     * @return 判断文件是否存在
     */
    public static boolean fileIsExists(String localFilePath){
        try{
            File f=new File(localFilePath);
            if(!f.exists()){
                return false;
            }

        }catch (Exception e) {
            // TODO: handle exception
            return false;
        }
        return true;
    }
}
